package com.yashi.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.github.pagehelper.PageInfo;
import com.yashi.entity.TbDepartment;
import com.yashi.service.DepartmentService;

@Controller
@RequestMapping("/dept")
public class DepartmentController {
	
	@Resource
	private DepartmentService departmentService;
	
	/**
	 * 添加部门
	 * @param department
	 * @return
	 */
	@RequestMapping("/addDept")
	public String addDept(TbDepartment department){
		try {
			departmentService.insertDept(department);
			return "redirect:/dept/findDeptList?pageNum=1";
		} catch (Exception e) {
			return e.getMessage();
		}
	}
	
	/**
	 * 查询所有部门
	 * @param request
	 * @return
	 */
	@RequestMapping("/findDeptList")
	public String findDeptList(HttpServletRequest request,Integer pageNum){
		try {
			PageInfo<TbDepartment> page = departmentService.findDeptList1(pageNum);
			request.setAttribute("page", page);
			List<TbDepartment> list = page.getList();
			request.setAttribute("findDeptList", list);
			
			/*return DeptList;*/
			return "deptList";
		} catch (Exception e) {
			return e.getMessage();
		}
	}
	
	/**
	 * 查询所有部门
	 * @param request
	 * @return
	 *//*
	@RequestMapping("/findDeptListToAdd")
	public String findDeptListToAdd(HttpServletRequest request){
		try {
			List<TbDepartment> findDeptList = departmentService.findDeptListToAdd();
			request.setAttribute("findDeptList", findDeptList);
			return "deptList";
		} catch (Exception e) {
			return e.getMessage();
		}
	}*/
	
	/**
	 * 模糊查询
	 * @param deptName
	 * @param request
	 * @return
	 */
	@RequestMapping("/SelectDeptByName")
	public String SelectDeptByName(String deptName,HttpServletRequest request,Integer pageNum){
		try {
			PageInfo<TbDepartment> page = departmentService.selectDeptByName(deptName,pageNum);
			request.setAttribute("page", page);
			request.setAttribute("findDeptList", page.getList());
			return "deptList";
		} catch (Exception e) {
			return e.getMessage();
		}
	}
	
	/**
	 * 为修改查询部门信息
	 * @param deptId
	 * @param request
	 * @return
	 */
	@RequestMapping("/findDeptById")
	public String findDeptById(String deptId,HttpServletRequest request){
		try {
			TbDepartment dept = departmentService.findDeptById(deptId);
			request.setAttribute("dept", dept);
			return "updateDept";
		} catch (Exception e) {
			return e.getMessage();
		}
	}
	
	/**
	 * 更新部门信息
	 * @param department
	 * @return
	 */
	@RequestMapping("/updateDept")
	public String updateDept(TbDepartment department){
		try {
			departmentService.updateDept(department);
			return "redirect:/dept/findDeptList?pageNum=1";
		} catch (Exception e) {
			return e.getMessage();
		}
	}
	
	/**
	 * 删除部门信息(逻辑删除)
	 * @param department
	 * @return
	 */
	@RequestMapping("/deleteDept")
	public String deleteDept(TbDepartment department){
		try {
			departmentService.deleteDept(department);
			return "redirect:/dept/findDeptList?pageNum=1";
		} catch (Exception e) {
			return e.getMessage();
		}
		
	}
	
	/**
	 * 跳转到添加部门页面
	 * @return
	 */
	@RequestMapping("/toAddDept")
	public String toAddDept(){
		return "addDept";
	}
	
}
